﻿using Dapper;
using HR.IServices.PengIService;
using HR.Models.PengModels;
using System.Data.SqlClient;

namespace HR.Services.PengService

{
    public class MianShiServices : IMianShiService
    {
        private readonly IConfiguration conf;
        private readonly string? conStr;

        public MianShiServices(IConfiguration conf)
        {
            this.conf = conf;
            conStr = this.conf["ConnectionStrings"];
        }

        public Task<List<MsJlLianCha>> LianChaer()
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Query<MsJlLianCha>($"select * from view_engage_er where check_status=2").ToList();
                }
            });
        }

        public Task<List<MsJlLianCha>> LiangBSel()
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Query<MsJlLianCha>($"select * from view_engage_er where check_status=1").ToList();
                }
            });
        }

        public Task<MianShi?> MianSFind(int resume_id)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    string sql = @$"select * from [dbo].engage_interview where [resume_id] ={resume_id}";
                    MianShi? ms = con.Query<MianShi>(sql).FirstOrDefault();
                    return ms;
                }
            });
        }

        /// <summary>
        /// 对简历表中的面试状态进行修改，然后再对面试表记录进行新增
        /// </summary>
        /// <param name="mianShi"></param>
        /// <returns></returns>
        public Task<int> MianShiAdd(MianShi mianShi)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    //修改简历表，新增面试表
                    string strudp = @$"UPDATE [dbo].[engage_resume] SET [interview_status] = 0 WHERE [res_id]= {mianShi.resume_id}";
                    con.Execute(strudp);
                    return con.Execute(@$"insert into [dbo].[engage_interview]
(resume_id,human_name,human_major_id,human_major_name,human_major_kind_id,human_major_kind_name,interview_amount,image_degree,native_language_degree,foreign_language_degree,response_speed_degree,EQ_degree,IQ_degree,multi_quality_degree,register,registe_time,interview_comment,check_status) 
values('{mianShi.resume_id}','{mianShi.human_name}','{mianShi.human_major_id}','{mianShi.human_major_name}','{mianShi.human_major_kind_id}','{mianShi.human_major_kind_name}','{mianShi.interview_amount}','{mianShi.image_degree}','{mianShi.native_language_degree}','{mianShi.foreign_language_degree}','{mianShi.response_speed_degree}','{mianShi.EQ_degree}','{mianShi.IQ_degree}','{mianShi.multi_quality_degree}','{mianShi.register}','{mianShi.registe_time}','{mianShi.interview_comment}','{mianShi.check_status}')");
                }
            });
        }

       

        public Task<List<MianShi>> ZtSel()
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Query<MianShi>($"select * from [dbo].[engage_interview] where check_status = 1").ToList();
                }
            });
        }
    }
}
